package com.liy.cloud.common.trade.entity;

import com.liy.cloud.common.exception.NegativeNumberException;
import com.liy.cloud.common.trade.utils.TradeMessageParser;

import java.io.Serializable;
import java.text.ParseException;

/**
 * 换票售票交易明细表实体类 作者: 日期：2018-08-12 17:14:52 描述：
 */
public class TradeExchange extends TradeComm implements Serializable {

	private static final long serialVersionUID = 1L;

	/*
	 * 主表ID
	 */
	private String commId;

	/*
	 * 有效起始时间
	 */
	private String beginDate;

	/*
	 * 有效终止时间
	 */
	private String endDate;

	/*
	 * 目的站点
	 */
	private String destinationSite;

	/*
	 * 前次交易终端代号
	 */
	private String exDeviceId;

	/*
	 * 前次交易类型编码
	 */
	private String exTradeCode;

	/*
	 * 前次交易时间
	 */
	private String exTradeTime;

	/*
	 * 前次交易金额
	 */
	private Long exTradeMoney;

	/*
	 * 前次交易操作员
	 */
	private String exOper;

	/*
	 * 关联票卡逻辑号
	 */
	private String relateCardId;

	/*
	 * 关联票卡余额
	 */
	private Long relateBalance;

	/*
	 * 关联票卡入站地点
	 */
	private String relateEntrance;

	/**
	 * 关联票卡入站时间
	 */
	private String relateTime;

	/*
	 * 换票退票金额
	 */
	private Long exchangeMoney;

	/*
	 * 换票退票地点
	 */
	private String exchangeAddress;

	/*
	 * 换票退票时间
	 */
	private String exchangeTime;

	/*
	 * 审核人ID
	 */
	private String operId;

	/**
	 * 默认构造方法
	 **/
	public TradeExchange() {
		super();
	}

	/**
	 * getger和setter方法
	 */

	public String getCommId() {
		return commId;
	}

	public void setCommId(String commId) {
		this.commId = commId;
	}

	public String getBeginDate() {
		return beginDate;
	}

	public void setBeginDate(String beginDate) throws ParseException {
		checkTimeStyle(beginDate, "BeginDate");
		this.beginDate = beginDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) throws ParseException {
		checkTimeStyle(endDate, "EndDate");
		this.endDate = endDate;
	}

	public String getDestinationSite() {
		return destinationSite;
	}

	public void setDestinationSite(String destinationSite) {
		this.destinationSite = destinationSite;
	}

	public String getExDeviceId() {
		return exDeviceId;
	}

	public void setExDeviceId(String exDeviceId) {
		this.exDeviceId = exDeviceId;
	}

	public String getExTradeCode() {
		return exTradeCode;
	}

	public void setExTradeCode(String exTradeCode) {
		this.exTradeCode = exTradeCode;
	}

	public String getExTradeTime() {
		return exTradeTime;
	}

	public void setExTradeTime(String exTradeTime) throws ParseException {
		checkTimeStyle(exTradeTime, "ExTradeTime");
		this.exTradeTime = exTradeTime;
	}

	public Long getExTradeMoney() {
		return exTradeMoney;
	}

	public void setExTradeMoney(Long exTradeMoney) throws NegativeNumberException {
		checkMoneyStyle(exTradeMoney, "ExTradeMoney");
		this.exTradeMoney = exTradeMoney;
	}

	public String getExOper() {
		return exOper;
	}

	public void setExOper(String exOper) {
		this.exOper = exOper;
	}

	public String getRelateCardId() {
		return relateCardId;
	}

	public void setRelateCardId(String relateCardId) {
		this.relateCardId = relateCardId;
	}

	public Long getRelateBalance() {
		return relateBalance;
	}

	public void setRelateBalance(Long relateBalance) {
		this.relateBalance = relateBalance;
	}

	public String getRelateEntrance() {
		return relateEntrance;
	}

	public void setRelateEntrance(String relateEntrance) {
		this.relateEntrance = relateEntrance;
	}

	public String getRelateTime() {
		return relateTime;
	}

	public void setRelateTime(String relateTime) {
		this.relateTime = relateTime;
	}

	public Long getExchangeMoney() {
		return exchangeMoney;
	}

	public void setExchangeMoney(Long exchangeMoney) throws NegativeNumberException {
		checkMoneyStyle(exchangeMoney, "ExchangeMoney");
		this.exchangeMoney = exchangeMoney;
	}

	public String getExchangeAddress() {
		return exchangeAddress;
	}

	public void setExchangeAddress(String exchangeAddress) {
		this.exchangeAddress = exchangeAddress;
	}

	public String getExchangeTime() {
		return exchangeTime;
	}

	public void setExchangeTime(String exchangeTime) throws ParseException {
		checkTimeStyle(exchangeTime, "ExchangeTime");
		this.exchangeTime = exchangeTime;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getOperId() {
		return operId;
	}

	public void setOperId(String operId) {
		this.operId = operId;
	}

	/**
	 * 根据版本容解析交易内容
	 * 
	 * @param message
	 * @throws Exception
	 */
	public TradeExchange init(String message) throws Exception {



		TradeMessageParser messageParser = null;
		if(message.startsWith("SZMETROV2")) {

			// 根据报文格式长度来定义， -1为行业分割符
			int[] splitNumber = { 9, 8, 9, 2, 10, 9, 6, 6, 14, 10, 8, 8, 8, 8, 5, 10, 14, 14, 10, 9, 2, 14, 8, 6, 9, 8, 10,
					14, 8, 10, 14, 20, 10, 2, 2, 16, 1, 1, 2, 2, 2, 4, 4, 1, 4, 5, -1, 5 };
			messageParser = new TradeMessageParser(message, splitNumber);
			this.setVersion(messageParser.getFiled(0));
			this.setTac(messageParser.getFiled(1));
			this.setCardId(messageParser.getFiled(2));
			this.setTradeCode(messageParser.getFiled(3));
			this.setTradeAddress(messageParser.getFiled(4));

			this.setDeviceId(messageParser.getFiled(5));
			this.setOperator(messageParser.getFiled(6));
			this.setOperId(messageParser.getFiled(7));
			this.setTradeTime(messageParser.getFiled(8));
			this.setDeviceSeq(Long.parseLong(messageParser.getFiled(9)));

			this.setTradeMoney(Long.parseLong(messageParser.getFiled(10)));
			this.setTradeValue(Long.parseLong(messageParser.getFiled(11)));
			this.setDeposit(Long.parseLong(messageParser.getFiled(12))); // 押金
			this.setCost(Long.parseLong(messageParser.getFiled(13)));
			this.setCardSeq(Long.parseLong(messageParser.getFiled(14)));

			this.setDeviceSum(Long.parseLong(messageParser.getFiled(15)));
			this.setBeginDate(messageParser.getFiled(16));
			this.setEndDate(messageParser.getFiled(17));
			this.setDestinationSite(messageParser.getFiled(18));
			this.setExDeviceId(messageParser.getFiled(19));

			this.setExTradeCode(messageParser.getFiled(20));
			this.setExTradeTime(messageParser.getFiled(21));
			this.setExTradeMoney(Long.parseLong(messageParser.getFiled(22)));
			this.setExOper(messageParser.getFiled(23));
			this.setRelateCardId(messageParser.getFiled(24));

			this.setRelateBalance(Long.parseLong(messageParser.getFiled(25)));
			this.setRelateEntrance(messageParser.getFiled(26));
			this.setRelateTime(messageParser.getFiled(27));
			this.setExchangeMoney(Long.parseLong(messageParser.getFiled(28)));
			this.setExchangeAddress(messageParser.getFiled(29));
			this.setExchangeTime(messageParser.getFiled(30));

			this.setSamcardId(messageParser.getFiled(31));
			this.setSamcardSeq(Long.parseLong(messageParser.getFiled(32)));
			this.setCardCode(messageParser.getFiled(33));
			this.setTradeSub(messageParser.getFiled(34));
			this.setPhysicalId(messageParser.getFiled(35));

			this.setTestFlag(messageParser.getFiled(36));
			this.setGreyFlag(messageParser.getFiled(37));
			this.setMedia(messageParser.getFiled(38));
			this.setCardVersion(messageParser.getFiled(39));
			this.setSubCardCode(messageParser.getFiled(40));

			this.setCityCode(messageParser.getFiled(41));
			this.setBusinessCode(messageParser.getFiled(42));

			this.setIndustrySplit(messageParser.getFiled(43));
			this.setIssuer(messageParser.getFiled(44));
			super.setIndustryDataLength(Long.parseLong(messageParser.getFiled(45)));
			super.setIndustryData(messageParser.getFiled(46));
			super.setCrc16(messageParser.getFiled(47));

		} else if(message.startsWith("SZMETROV3")) {

			/** 根据报文格式长度来定义， -1为行业分割符
			 * 一、V3卡号由9位变为20位
			 * 二、增加字段：
			 付费方式
			 支付方式
			 发卡方机构码
			 发卡方交易流水号
			 运营模式
			 三、票卡发行方标识	从长度4变为长度2
			 */
			int[] splitNumber = {
					9, 8, 20, 2, 10,   9, 6, 6, 14, 10,   8, 8, 8, 8, 5,   10, 14, 14, 10, 9,   2, 14, 8, 6, 9,
					8, 10, 14, 8, 10,  14, 20, 10, 2, 2,  16, 1, 1, 2, 2,   2, 4, 4,
					1, 2, 8, 10, 4,    //V3增加的字段
					1, 2, 5,
					-1, 5};
			messageParser = new TradeMessageParser(message, splitNumber);
			this.setVersion(messageParser.getFiled(0));
			this.setTac(messageParser.getFiled(1));
			this.setCardId(messageParser.getFiled(2));
			this.setTradeCode(messageParser.getFiled(3));
			this.setTradeAddress(messageParser.getFiled(4));

			this.setDeviceId(messageParser.getFiled(5));
			this.setOperator(messageParser.getFiled(6));
			this.setOperId(messageParser.getFiled(7));
			this.setTradeTime(messageParser.getFiled(8));
			this.setDeviceSeq(Long.parseLong(messageParser.getFiled(9)));

			this.setTradeMoney(Long.parseLong(messageParser.getFiled(10)));
			this.setTradeValue(Long.parseLong(messageParser.getFiled(11)));
			this.setDeposit(Long.parseLong(messageParser.getFiled(12))); // 押金
			this.setCost(Long.parseLong(messageParser.getFiled(13)));
			this.setCardSeq(Long.parseLong(messageParser.getFiled(14)));

			this.setDeviceSum(Long.parseLong(messageParser.getFiled(15)));
			this.setBeginDate(messageParser.getFiled(16));
			this.setEndDate(messageParser.getFiled(17));
			this.setDestinationSite(messageParser.getFiled(18));
			this.setExDeviceId(messageParser.getFiled(19));

			this.setExTradeCode(messageParser.getFiled(20));
			this.setExTradeTime(messageParser.getFiled(21));
			this.setExTradeMoney(Long.parseLong(messageParser.getFiled(22)));
			this.setExOper(messageParser.getFiled(23));
			this.setRelateCardId(messageParser.getFiled(24));

			this.setRelateBalance(Long.parseLong(messageParser.getFiled(25)));
			this.setRelateEntrance(messageParser.getFiled(26));
			this.setRelateTime(messageParser.getFiled(27));
			this.setExchangeMoney(Long.parseLong(messageParser.getFiled(28)));
			this.setExchangeAddress(messageParser.getFiled(29));
			this.setExchangeTime(messageParser.getFiled(30));

			this.setSamcardId(messageParser.getFiled(31));
			this.setSamcardSeq(Long.parseLong(messageParser.getFiled(32)));
			this.setCardCode(messageParser.getFiled(33));
			this.setTradeSub(messageParser.getFiled(34));
			this.setPhysicalId(messageParser.getFiled(35));

			this.setTestFlag(messageParser.getFiled(36));
			this.setGreyFlag(messageParser.getFiled(37));
			this.setMedia(messageParser.getFiled(38));
			this.setCardVersion(messageParser.getFiled(39));
			this.setSubCardCode(messageParser.getFiled(40));

			this.setCityCode(messageParser.getFiled(41));
			this.setBusinessCode(messageParser.getFiled(42));

			// V3增加字段
			this.setPayMethod(messageParser.getFiled(43));
			this.setPayType(messageParser.getFiled(44));
			this.setIssuerCode(messageParser.getFiled(45));
			this.setIssuerSeq(messageParser.getFiled(46));
			this.setOperMode(messageParser.getFiled(47));

			this.setIndustrySplit(messageParser.getFiled(48));
			this.setIssuer(messageParser.getFiled(49));
			super.setIndustryDataLength(Long.parseLong(messageParser.getFiled(50)));
			super.setIndustryData(messageParser.getFiled(51));
			super.setCrc16(messageParser.getFiled(52));
		}
		return this;
	}
}